{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "file1 = open('../datasets/AAPD/llama_selected_label_1_user.txt', 'r')\n",
    "raw_label_set = file1.readlines()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "label_set = []\n",
    "for row in raw_label_set:\n",
    "    label_set.append(row.split(': ')[1].strip())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Parallel and Distributed Computing',\n",
       " 'Information Theory',\n",
       " 'Quantum Information Processing',\n",
       " 'Compressed Sensing',\n",
       " 'Information Retrieval',\n",
       " 'Community Detection in Networks',\n",
       " 'Cryptography and Information Security',\n",
       " 'Network Optimization',\n",
       " 'Error Correction Codes',\n",
       " 'Massive MIMO and Channel Estimation',\n",
       " 'Spectrum Sensing and Cognitive Radio Networks',\n",
       " 'Algorithmic Game Theory',\n",
       " 'Programming Languages',\n",
       " 'Image Processing',\n",
       " 'Computer Science - Algorithms',\n",
       " 'Coding Theory',\n",
       " 'Information Retrieval and Clustering',\n",
       " 'Error Correction and FEC Codes',\n",
       " 'Computational Models and Software Development',\n",
       " 'Machine Learning',\n",
       " 'Control Theory',\n",
       " 'Distributed Computing',\n",
       " 'Signal Processing and Machine Learning',\n",
       " 'Wireless Communications',\n",
       " 'Mobile Cloud Computing',\n",
       " 'Formal Verification of Software Systems',\n",
       " 'Logic and Probability',\n",
       " 'Probabilistic Programming and Inference',\n",
       " 'Algorithmic Combinatorics',\n",
       " 'Robotics',\n",
       " 'Algorithms for Graphs',\n",
       " 'Constraint Satisfaction Problems',\n",
       " 'Network Analysis',\n",
       " 'Bioinformatics',\n",
       " 'Bibliometrics and Research Evaluation',\n",
       " 'Communication Theory',\n",
       " 'Traffic and Transportation',\n",
       " 'Community Detection in Networks',\n",
       " 'Natural Language Processing',\n",
       " 'Human-Computer Interaction',\n",
       " 'Election Systems and Voting Theory',\n",
       " 'Optimization',\n",
       " 'Data Privacy and Security',\n",
       " 'Graph Theory',\n",
       " 'Media Streaming and Quality of Experience',\n",
       " 'Information Security',\n",
       " 'Computational Geometry',\n",
       " 'Communication Networks',\n",
       " 'Data Science',\n",
       " 'Computer Vision',\n",
       " 'Mathematical Structures',\n",
       " 'Bayesian Decision Theory and Markov Decision Processes',\n",
       " 'Formal Language Theory and Automata Theory',\n",
       " 'Wireless Networks']"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "label_set"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "action = {'A': 'delete', 'B': 'split', 'C': 'change', 'D': 'merge', 'E': 'No action'}\n",
    "prompts = 'Please follow all the rules below and keep the original index. \\n'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Asking to truncate to max_length but no maximum length is provided and the model has no predefined maximum length. Default to no truncation.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'sequence': 'Leo Messi is the best player ever.', 'labels': ['sports', 'politics', 'science', 'environment'], 'scores': [0.9176685214042664, 0.6474247574806213, 0.5379259586334229, 0.3733513057231903]}\n",
      "Predicted class: sports\n"
     ]
    }
   ],
   "source": [
    "from transformers import pipeline\n",
    "\n",
    "zstc = pipeline(\"zero-shot-classification\", model=\"BSC-LT/sciroshot\")\n",
    "\n",
    "sentence = \"Leo Messi is the best player ever.\"\n",
    "candidate_labels = [\"politics\", \"science\", \"sports\", \"environment\"]\n",
    "template = \"This example is {}\"\n",
    "\n",
    "output = zstc(sentence, candidate_labels, hypothesis_template=template, multi_label=True)\n",
    "\n",
    "print(output)\n",
    "print(f'Predicted class: {output[\"labels\"][0]}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'sequence': 'the alignment of a set of objects by means of transformations plays an important role in computer vision whilst the case for only two objects can be solved globally , when multiple objects are considered usually iterative methods are used in practice the iterative methods perform well if the relative transformations between any pair of objects are free of noise however , if only noisy relative transformations are available \\\\( e g due to missing data or wrong correspondences \\\\) the iterative methods may fail based on the observation that the underlying noise free transformations can be retrieved from the null space of a matrix that can directly be obtained from pairwise alignments , this paper presents a novel method for the synchronisation of pairwise transformations such that they are transitively consistent simulations demonstrate that for noisy transformations , a large proportion of missing data and even for wrong correspondence assignments the method delivers encouraging results', 'labels': ['Computer Vision', 'Computer Science - Algorithms', 'Image Processing', 'Computational Geometry', 'Algorithms for Graphs', 'Graph Theory', 'Information Theory', 'Algorithmic Combinatorics', 'Bayesian Decision Theory and Markov Decision Processes', 'Signal Processing and Machine Learning', 'Algorithmic Game Theory', 'Coding Theory', 'Optimization', 'Logic and Probability', 'Quantum Information Processing', 'Information Retrieval', 'Error Correction Codes', 'Machine Learning', 'Data Science', 'Formal Language Theory and Automata Theory', 'Information Retrieval and Clustering', 'Probabilistic Programming and Inference', 'Compressed Sensing', 'Error Correction and FEC Codes', 'Natural Language Processing', 'Mathematical Structures', 'Network Analysis', 'Control Theory', 'Computational Models and Software Development', 'Network Optimization', 'Constraint Satisfaction Problems', 'Massive MIMO and Channel Estimation', 'Distributed Computing', 'Formal Verification of Software Systems', 'Communication Theory', 'Bioinformatics', 'Parallel and Distributed Computing', 'Robotics', 'Human-Computer Interaction', 'Programming Languages', 'Communication Networks', 'Community Detection in Networks', 'Community Detection in Networks', 'Bibliometrics and Research Evaluation', 'Mobile Cloud Computing', 'Wireless Communications', 'Spectrum Sensing and Cognitive Radio Networks', 'Wireless Networks', 'Cryptography and Information Security', 'Information Security', 'Data Privacy and Security', 'Media Streaming and Quality of Experience', 'Election Systems and Voting Theory', 'Traffic and Transportation'], 'scores': [0.955618143081665, 0.9152524471282959, 0.8718089461326599, 0.8597100973129272, 0.852741003036499, 0.8085184097290039, 0.699556827545166, 0.6971257328987122, 0.6776525378227234, 0.6498916745185852, 0.6167068481445312, 0.61148601770401, 0.5843815207481384, 0.5831825733184814, 0.5794410109519958, 0.5781789422035217, 0.5767613649368286, 0.5730521082878113, 0.5554595589637756, 0.5475112199783325, 0.5469444394111633, 0.5230456590652466, 0.476551353931427, 0.47631630301475525, 0.4666423201560974, 0.4507153034210205, 0.4495712220668793, 0.41996142268180847, 0.4085811674594879, 0.3997317850589752, 0.3981729745864868, 0.3820733428001404, 0.36000415682792664, 0.35003089904785156, 0.34895893931388855, 0.342960923910141, 0.34155043959617615, 0.34069928526878357, 0.3384016454219818, 0.3283538818359375, 0.30786725878715515, 0.30737099051475525, 0.30737099051475525, 0.29279690980911255, 0.2514278292655945, 0.21954551339149475, 0.21010273694992065, 0.17387723922729492, 0.1680193692445755, 0.15122342109680176, 0.1326657384634018, 0.1250113546848297, 0.10496582090854645, 0.10251551866531372]}\n",
      "Predicted class: Computer Vision\n"
     ]
    }
   ],
   "source": [
    "sentence = \"the alignment of a set of objects by means of transformations plays an important role in computer vision whilst the case for only two objects can be solved globally , when multiple objects are considered usually iterative methods are used in practice the iterative methods perform well if the relative transformations between any pair of objects are free of noise however , if only noisy relative transformations are available \\( e g due to missing data or wrong correspondences \\) the iterative methods may fail based on the observation that the underlying noise free transformations can be retrieved from the null space of a matrix that can directly be obtained from pairwise alignments , this paper presents a novel method for the synchronisation of pairwise transformations such that they are transitively consistent simulations demonstrate that for noisy transformations , a large proportion of missing data and even for wrong correspondence assignments the method delivers encouraging results\"\n",
    "candidate_labels = label_set\n",
    "template = \"This example is {}\"\n",
    "\n",
    "output = zstc(sentence, candidate_labels, hypothesis_template=template, multi_label=True)\n",
    "\n",
    "print(output)\n",
    "print(f'Predicted class: {output[\"labels\"][0]}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'sequence': 'I have a problem with my iphone that needs to be resolved asap!',\n",
       " 'labels': ['urgent', 'phone', 'computer', 'not urgent', 'tablet'],\n",
       " 'scores': [0.522757351398468,\n",
       "  0.45814093947410583,\n",
       "  0.014264650642871857,\n",
       "  0.0026850069407373667,\n",
       "  0.002152062952518463]}"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from transformers import pipeline\n",
    "\n",
    "pipe = pipeline(model=\"facebook/bart-large-mnli\")\n",
    "pipe(\"I have a problem with my iphone that needs to be resolved asap!\",\n",
    "    candidate_labels=[\"urgent\", \"not urgent\", \"phone\", \"tablet\", \"computer\"],\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "example_prompt = \"\"\"\n",
    "\n",
    "Do you think a new label \"International Development and Aid\" should be split into new labels? Please answer YES or NO. If YES, please print the new label only.\n",
    "<<ANSWER>> YES International Development, Aid <</ANSWER>>\n",
    "Do you think a new label \"Fisheries Management\" should be included in the above label set? Please only answer YES or NO.\n",
    "<<ANSWER>> NO <</ANSWER>>\n",
    "[/INST]\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "example_prompt = \"\"\"\n",
    "\n",
    "Please delete the label has similar meanings with Information Theory, Quantum Information Processing, Compressed Sensing, Network Optimization, Railway Transportation\n",
    "\n",
    "Do you think a new label \"Optimization\" should be included in the above label set? Please only answer YES or NO.\n",
    "<<ANSWER>> no <</ANSWER>>\n",
    "[/INST]\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Multi-Label",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
